<?php
declare (strict_types = 1);

namespace app\admin\business;

use think\Exception;
use think\facade\Log;

class ProductSpecValue {

    protected $model;

    public function __construct() {

        $this->model = new \app\model\mysql\ProductSpecValue();
    }


    public function save($data = []) : bool {
        if (empty($data["spec_id"]) || empty($data["name"])) {
            return false;
        }
        $isExist = false;
        try{
            $isExist = $this->model->isExistSpecValue($data["spec_id"], $data["name"]);
        }catch (Exception $e){
            Log::error($e);
            return false;
        }
        if ($isExist) {
            throw new Exception("该规格的属性值已经存在了");
        }

        $isSaveSuccess = false;
        try{
            $isSaveSuccess = $this->model->saveData($data);
        }catch (Exception $e){
            Log::error($e);
            return false;
        }

        return $isSaveSuccess;
    }
}